/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.repository;

import me.zhengjie.modules.anniversary.domain.Department;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
* @website https://el-admin.vip
* @author Jiayin Wei
* @date 2020-07-22
**/
public interface DepartmentRepository extends JpaRepository<Department, Long>, JpaSpecificationExecutor<Department> {

    /**
     * 获取院系架构的可选年份
     * @return /
     */
    @Query(value = "SELECT DISTINCT year FROM gzmu_ann_department ORDER BY year",nativeQuery = true)
    List<Integer> getDepartmentYears();

    /**
     * 获取院系架构的可选年份
     * @return /
     */
    @Query(value = "SELECT DISTINCT(`year`) FROM v_department WHERE current_name=?1 ORDER BY `year`",nativeQuery = true)
    List<Integer> getCollegeYears(String college);
    /**
     * 根据 ParentId 查询
     * @param id parent_id
     * @return /
     */
    List<Department> findByParentId(Long id);

    /**
     * 获取顶级部门
     * @return /
     */
    List<Department> findByParentIdIsNull();

    /**
     * 判断是否存在子节点
     * @param parent_id /
     * @return /
     */
    int countByParentId(Long parent_id);

    /**
     * 根据ID更新sub_count
     * @param count /
     * @param id /
     */
    @Modifying
    @Query(value = " update gzmu_ann_department set sub_count = ?1 where id = ?2 ",nativeQuery = true)
    void updateSubCntById(Integer count, Long id);

    @Query(value = "SELECT * FROM gzmu_ann_department WHERE year=?1 AND type='2'",nativeQuery = true)
    List<Department> findMajorsByYear(Integer year);

    @Query(value = "SELECT * FROM gzmu_ann_department WHERE name=?1 AND is_enable=1",nativeQuery = true)
    List<Department> findCollegesByName(String Name);

    @Query(value = "SELECT * FROM gzmu_ann_department WHERE year=?1 AND type='0'",nativeQuery = true)
    List<Department> findCollegesByYear(Integer year);

    @Query(value = "SELECT * FROM gzmu_ann_department WHERE name=?1 AND `year`=?2 AND is_enable=1",nativeQuery = true)
    List<Department> findCollegesByYearV(String college, Integer year);

    @Query(value = "SELECT id FROM gzmu_ann_department WHERE year=:year and name=:name",nativeQuery = true)
    String getDeparmentID(@Param("name") String name, @Param("year") Integer year);

    //根据年份与父级id,当前的类型
    @Query(value = "SELECT * FROM gzmu_ann_department WHERE year=:year and parent_id=:nameId and type=:type",nativeQuery = true)
    List<Department> getDepartmentName(@Param("year") Integer year, @Param("nameId") Integer nameId, @Param("type") Integer type);

    @Query(value = "SELECT name FROM gzmu_ann_department WHERE year=?1 AND type='2'",nativeQuery = true)
    List<String> findMajorsNameByYear(Integer year);

    /**
     * 根据年份和学院名称查询学院信息
     * @param year 年份
     * @param name 学院
     * @return
     */
    @Query(value = "SELECT * FROM gzmu_ann_department WHERE year=?1  and name=?2 AND type='0' LIMIT 0,1 ",nativeQuery = true)
    Department findCollegesByYearName(Integer year ,String name);

    /**
     * 根据学院、年份、专业名称查询专业信息
     * @param year
     * @param name
     * @param collegeId
     * @return
     */
    @Query(value = "SELECT * FROM gzmu_ann_department WHERE year=?1 and name=?2 and parent_id=?3  AND type='2' LIMIT 0,1",nativeQuery = true)
    Department findMajorsByYearName(Integer year,String name,Integer collegeId);
}
